package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.CourseVideo;
import com.dingreading.cloud.admin.entity.table.CourseVideoTableDef;
import com.dingreading.cloud.admin.mapper.CourseVideoMapper;
import com.dingreading.cloud.admin.service.CourseVideoService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CourseVideoServiceImpl extends BaseServiceImpl<CourseVideoMapper, CourseVideo, CourseVideoTableDef> implements CourseVideoService {

    @Override
    protected CourseVideoTableDef getTable() {
        return CourseVideoTableDef.courseVideo;
    }

    @Override
    public Page<CourseVideo> pageList(PageUtil pageUtil, Long courseId, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (courseId != null)
            condition.and(table.courseId.eq(courseId));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.name.like(keyword));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.enabled.desc(), table.orderNum.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public int getMaxShowOrder() {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .from(table);

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);

        return NepUtil.nullToZero(integer);
    }

    @Override
    public void updateEnabled(Long id, Integer enabled) {
        UpdateChain.of(CourseVideo.class)
                .set(table.enabled, enabled)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<CourseVideo> getByCourseId(Long courseId, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.courseId.eq(courseId));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());
        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public long getCourseIdCnt(Long courseId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.courseId.eq(courseId));

        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public long dropBean(Long id) {
        return getMapper().deleteByCondition(table.id.eq(id));
    }

}
